package com.erp.app.util.web;

import java.util.HashMap;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;

import com.erp.framework.SimpleConstants;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.StringUtil;

public class ActionErrorUtil implements SimpleConstants {
	public static HashMap errorMsgMap = new HashMap();
	
	
	public static String ERROR_GENERAL_CRITERIA_BLANK = "ERROR_GENERAL_CRITERIA_BLANK";
	public static String ERROR_GENERAL_DESCRIPTION_BLANK = "ERROR_GENERAL_DESCRIPTION_BLANK";
	public static String ERROR_GENERAL_FIELD_BLANK = "ERROR_GENERAL_FIELD_BLANK";
	public static String ERROR_INVALID_YEAR_FORMAT = "ERROR_INVALID_YEAR_FORMAT";
	public static String ERROR_INVALID_MONTH_FORMAT = "ERROR_INVALID_MONTH_FORMAT";
	public static String ERROR_INVALID_MONTHYEAR_FORMAT = "ERROR_INVALID_MONTHYEAR_FORMAT";
	public static String ERROR_INVALID_DATE_FORMAT = "ERROR_INVALID_DATE_FORMAT";
	public static String ERROR_INVALID_TIME_FORMAT = "ERROR_INVALID_TIME_FORMAT";
	public static String ERROR_INVALID_DATE_DATETIME_FORMAT = "ERROR_INVALID_DATE_DATETIME_FORMAT";
	public static String ERROR_INVALID_DATE_TIME_FORMAT = "ERROR_INVALID_DATE_TIME_FORMAT";
	public static String ERROR_INVALID_NUMBER_FORMAT = "ERROR_INVALID_NUMBER_FORMAT";
	public static String ERROR_INVALID_POSITIVE_INTEGER_FORMAT = "ERROR_INVALID_POSITIVE_INTEGER_FORMAT";
	public static String ERROR_INVALID_POSITIVE_NUMBER_FORMAT = "ERROR_INVALID_POSITIVE_NUMBER_FORMAT";
	public static String ERROR_INVALID_NEGATIVE_INTEGER_FORMAT = "ERROR_INVALID_NEGATIVE_INTEGER_FORMAT";
	public static String ERROR_INVALID_NEGATIVE_NUMBER_FORMAT = "ERROR_INVALID_NEGATIVE_NUMBER_FORMAT";
	public static String ERROR_INVALID_NON_NEGATIVE_INTEGER_FORMAT = "ERROR_INVALID_NON_NEGATIVE_INTEGER_FORMAT";
	public static String ERROR_INVALID_NON_NEGATIVE_NUMBER_FORMAT = "ERROR_INVALID_NON_NEGATIVE_NUMBER_FORMAT";
	public static String ERROR_INVALID_FROM_GREATER_THAN_TO = "ERROR_INVALID_FROM_GREATER_THAN_TO";
	public static String ERROR_INVALID_CODE = "ERROR_INVALID_CODE";
	public static String ERROR_INVALID_EMAIL = "ERROR_INVALID_EMAIL";
	public static String ERROR_DUPLICATE_KEY = "ERROR_DUPLICATE_KEY";
	public static String ERROR_LINE_NO = "ERROR_LINE_NO";
    public static final String ERROR_INVALID_NDC_DATE = "ERROR_INVALID_NDC_DATE";

	private static final String ERROR_MSG_LI = "error.msg.li";
	public static final String ERROR_CHILD_IND = "$$";
	public static final String ERROR_INVALID_INTEGER_FORMAT = "ERROR_INVALID_INTEGER_FORMAT";

	public static final char[] INVALID_CODE_CHARACTERS = new char[] { ' ', '%', '?' };
    public static final char[] INVALID_CODE_CHARACTERS_WITHOUT_SPACE = new char[] { '%', '?' };
	public static final String INVALID_CODE_CHARACTERS_AS_STRING = CommonUtil.join(INVALID_CODE_CHARACTERS, "'", ", ");
    public static final String INVALID_CODE_CHARACTERS_WITHOUT_SPACE_AS_STRING = CommonUtil.join(INVALID_CODE_CHARACTERS_WITHOUT_SPACE, "'", ", ");

	// /**
	// *
	// * @deprecated
	// * @see Use ActionErrorUtil.addErrorForSpecficPurpose() instead
	// *
	// */
	// public static void addErrorForGeneral(ActionErrors errors, String msg) {
	// errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, msg));
	// }
	//
	// /**
	// *
	// * @deprecated
	// * @see Use ActionErrorUtil.addErrorForSpecficPurpose() instead
	// *
	// */
	// public static void addErrorForMsg(ActionErrors errors, String msg) {
	// errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, msg));
	// }
	//
	// /**
	// *
	// * @deprecated
	// * @see Use ActionErrorUtil.addErrorForSpecficPurpose() instead
	// *
	// */
	// public static void addErrorForExceptionGeneral(ActionErrors errors, String msg) {
	// errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, msg));
	// }
	//
	// /**
	// *
	// * @deprecated
	// * @see Use ActionErrorUtil.addErrorForSpecficPurpose() instead
	// *
	// */
	// public static void addErrorForExceptionError(ActionErrors errors, String msg) {
	// errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, msg));
	// }
	//
	// /**
	// *
	// * @deprecated
	// * @see Use ActionErrorUtil.addErrorForSpecficPurpose() instead
	// *
	// */
	// public static void addErrorForExceptionWarn(ActionErrors errors, String msg) {
	// errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, msg));
	// }
	//
	// /**
	// *
	// * @deprecated
	// * @see Use ActionErrorUtil.addErrorForSpecficPurpose() instead
	// *
	// */
	// public static void addErrorForExceptionDbError(ActionErrors errors, String msg) {
	// errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, msg));
	// }
	//
	// /**
	// *
	// * @deprecated
	// * @see Use ActionErrorUtil.addErrorForSpecficPurpose() instead
	// *
	// */
	// public static void addErrorForExceptionDbWarn(ActionErrors errors, String msg) {
	// errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, msg));
	// }

	public static void addErrorForSpecficPurpose(ActionErrors errors, String msg) {
		String newMsg = StringUtil.replace(msg, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, newMsg));
	}

	public static void addErrorForGeneralCriteriaBlank(ActionErrors errors) {
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_GENERAL_CRITERIA_BLANK));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_GENERAL_CRITERIA_BLANK)));
	}

	public static void addErrorForGeneralDescriptionBlank(ActionErrors errors) {
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_GENERAL_DESCRIPTION_BLANK));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_GENERAL_DESCRIPTION_BLANK)));
	}

	public static void addErrorForInvlalidCode(ActionErrors errors, String field) {
        addErrorForInvlalidCode(errors, field, INVALID_CODE_CHARACTERS);
	}

    public static void addErrorForInvlalidCode(ActionErrors errors, String field, char[] invalidCharacters) {
        String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
        //errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_CODE + " - " + INVALID_CODE_CHARACTERS_AS_STRING + ": " + newMsg));
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_CODE) + " - "
                + CommonUtil.join(invalidCharacters, "'", ",") + ": " + newMsg));
    }

	public static void addErrorForInvlalidEmailAddress(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_EMAIL + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_EMAIL) + ": " + newMsg));
	}

	public static void addErrorForGeneralFieldBlank(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_GENERAL_FIELD_BLANK + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_GENERAL_FIELD_BLANK) + ": " + newMsg));
	}

	public static void addErrorForInvalidYear(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_YEAR_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_YEAR_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidMonth(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_MONTH_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_MONTH_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidMonthYear(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_MONTHYEAR_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_MONTHYEAR_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidDate(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_DATE_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_DATE_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidTime(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_TIME_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_TIME_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidDateOrDatetime(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_DATE_DATETIME_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_DATE_DATETIME_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidDatetime(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_DATE_TIME_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_DATE_TIME_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidNumber(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_NUMBER_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_NUMBER_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidPositiveInteger(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_POSITIVE_INTEGER_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_POSITIVE_INTEGER_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidPositiveNumber(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_POSITIVE_NUMBER_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_POSITIVE_NUMBER_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidNegativeInteger(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_NEGATIVE_INTEGER_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_NEGATIVE_INTEGER_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidNegativeNumber(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_NEGATIVE_NUMBER_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_NEGATIVE_NUMBER_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidNonNegativeInteger(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_NON_NEGATIVE_INTEGER_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_NON_NEGATIVE_INTEGER_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidNonNegativeNumber(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_NON_NEGATIVE_NUMBER_FORMAT + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_NON_NEGATIVE_NUMBER_FORMAT) + ": " + newMsg));
	}

	public static void addErrorForInvalidFromGreaterThanTo(ActionErrors errors, String field) {
		String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_INVALID_FROM_GREATER_THAN_TO + ": " + newMsg));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_FROM_GREATER_THAN_TO) + ": " + newMsg));
	}

	public static void addErrorForDuplicateKey(ActionErrors errors, Object pk) {
		//errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, ERROR_DUPLICATE_KEY + ": " + pk));
		errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_DUPLICATE_KEY) + ": " + pk));
	}

    public static void addErrorForInvalidInteger(ActionErrors errors, String field) {
        String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_INTEGER_FORMAT) + ": " + newMsg));
    }

    public static void addErrorForInvalidNdcDate(ActionErrors errors, String field) {
        String newMsg = StringUtil.replace(field, ERROR_CHILD_IND, (String)errorMsgMap.get(ERROR_LINE_NO));
        errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(ERROR_MSG_LI, (String)errorMsgMap.get(ERROR_INVALID_NDC_DATE) + ": " + newMsg));
    }

}
